﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Management.Automation;
//using System.Text;
//using System.Web.Services.Protocols;

//namespace SPWebShell.cmdlet
//{
//    [Cmdlet(VerbsData.Update, "SPWorkflowMarkup")]
//    public class SPValidateWorkflowMarkup : CmdLetBase
//    {
//        ISPRepository _repos;
//        public SPValidateWorkflowMarkup()
//        {
//            Flag = 1;
//        }
//        [Parameter(Mandatory = true, HelpMessage = "The absolute URL to the web site where the List is located or any item/view within the List. e.g. http://server/sites/SiteA/doclib1/Allitems.aspx")]
//        [ValidateNotNullOrEmpty]
//        public string Url { get; set; }

//        [Parameter(Mandatory = true, HelpMessage = "The path on disk or absolute url to the file which contains the markup contents. Usually has .XOML file extension. E.g .\test.xoml or http://server/sites/SiteA/workflowstest.xoml")]
//        [ValidateNotNullOrEmpty]
//        [Alias("markup")]
//        public string FileMarkup { get; set; }

//        [Parameter(Mandatory = true, HelpMessage = "The path on disk or absolute url to the file which contains the rules. Usually has the .XOML.RULES extension")]
//        [ValidateNotNullOrEmpty]
//        [Alias("rules")]
//        public string FileRules { get; set; }
        
//        [Parameter(Mandatory = true, HelpMessage = "The path on disk or absolute url to the file which contains the assosciation data. e.g. The config file in SharePoint Designer workflows")]
//        [ValidateNotNullOrEmpty]
//        [Alias("config")]
//        public string FileConfig { get; set; }

//        [Parameter(Mandatory = true, HelpMessage = "Should be either 1(only validations) or 2(validate and create the support objects). " )]
//        [ValidateNotNullOrEmpty]
//        [ValidatePattern("^[12]{1}$")]
//        public int Flag { get; set; }


//        protected override void ProcessRecord()
//        {
//            try
//            {
//                VerboseLogger("Start time: " + DateTime.Now.ToString());
//                _repos = base.SPRepos;
//                string weburl = _repos.GetContainerWebFromDocLibUrl(this.Url);
//                string contentsMarkup = this.GetFileContents(this.FileMarkup);
//                string contentsRules = this.GetFileContents(this.FileRules);
//                string contentsConfig = this.GetFileContents(this.FileConfig);
//                string results = _repos.ValidateWorkflow(weburl, contentsMarkup, contentsRules, contentsConfig, this.Flag.ToString());
//                WriteObject(results);
//                VerboseLogger("End time: " + DateTime.Now.ToString());
//                base.ProcessRecord();
//            }
//            catch (SoapException ex)
//            {
//                string message = ex.Detail.OuterXml;
//                throw new Exception(message, ex);
//            }
//            finally
//            {
//                WriteVerbose("End time: " + DateTime.Now.ToString());
//            }
//        }
//        /// <summary>
//        /// If the 'file' represents an URL then read through HTTP otherwise treat as a local file.
//        /// </summary>
//        /// <param name="file"></param>
//        /// <returns></returns>
//        private string GetFileContents(string file)
//        {
//            Uri urlFile = null;
//            if (file.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))
//            {
//                urlFile = new Uri(file, UriKind.Absolute);
//                System.Net.WebClient cl = new System.Net.WebClient();
//                cl.Credentials = System.Net.CredentialCache.DefaultCredentials;
//                return cl.DownloadString(file);
//            }
//            else
//            {
//                string pathfull = base.GetFullPathToFile(file);
//                return System.IO.File.ReadAllText(file);
//            }
//        }
//        private void ValidaFileExistence(string filepath)
//        {
//            if (!System.IO.File.Exists(filepath))
//            {
//                string message=string.Format("The file: {0} was not found",filepath);
//                throw new Exception(message);
//            }
//        }
//    }
//}
